/**
 * 
 */
package com.thinkgsp.jxc.service.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgsp.jxc.model.RpAPayBill;
import com.thinkgsp.jxc.model.RpARecBill;
import com.thinkgsp.jxc.model.RpArpBill;
import com.thinkgsp.jxc.model.RpBillDetail;
import com.thinkgsp.jxc.model.RpContact;
import com.thinkgsp.jxc.model.RpPayBill;
import com.thinkgsp.jxc.model.RpRecBill;
import com.thinkgsp.jxc.persistence.mybatis.PagingResult;
import com.thinkgsp.jxc.service.RpBillService;
import com.thinkgsp.jxc.service.ServiceException;
import com.thinkgsp.sys.model.SysAccountPeriod;

/**
 * @author evan
 *
 */
@Service("rpBillService")
@Transactional
public class RpBillServiceImpl extends RpServiceImpl implements
		RpBillService {

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpBillService#getRecBill(java.lang.Long)
	 */
	public RpRecBill getRecBill(Long billId) {
		return rpBillMapper.getRpBill(billId);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpBillService#getPayBill(java.lang.Long)
	 */
	public RpPayBill getPayBill(Long billId) {
		return rpBillMapper.getRpBill(billId);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpBillService#getRecBillList(java.sql.Date, java.sql.Date)
	 */
	public List<RpRecBill> getRecBillList(Date begDate, Date endDate) {
		return rpBillMapper.getRpBillList(begDate, endDate, 1);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpBillService#getPayBillList(java.sql.Date, java.sql.Date)
	 */
	public List<RpPayBill> getPayBillList(Date begDate, Date endDate) {
		return rpBillMapper.getRpBillList(begDate, endDate, -1);
	}


	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpBillService#createRecBillByArecBill(java.lang.Long)
	 */
	public RpRecBill createRecBillByArecBill(Long arecBillId)
			throws ServiceException {
		RpARecBill arecBill = arpBillMapper.getArpBill(arecBillId);
		RpRecBill recBill = new RpRecBill();
		recBill.setCustomer(arecBill.getCustomer());
		recBill.setDept(arecBill.getDept());
		recBill.setEmp(arecBill.getEmp());
		recBill.setMaker(getCurrentEmp());
		recBill.setAmount(arecBill.getAmount());
		SysAccountPeriod period = getCurrentPeriod();
		recBill.setFyear(period.getFyear());
		recBill.setPeriod(period.getPeriod());
		RpBillDetail detail = new RpBillDetail();
		RpContact contact = contactMapper.findRpContactByArpBill(arecBillId);
		if(contact != null) {
			detail.setContact(contact);
			detail.setSettleAmount(contact.getRemAmount());
			recBill.setAmount(contact.getRemAmount());
		} else {
			//detail.setArpBillId(arecBillId);
			detail.setArpBill(arecBill);
			detail.setSettleAmount(arecBill.getAmount());
		}
		recBill.getDetails().add(detail);
		return recBill;
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpBillService#createPayBillByApayBill(java.lang.Long)
	 */
	public RpPayBill createPayBillByApayBill(Long apayBillId)
			throws ServiceException {
		RpAPayBill apayBill = arpBillMapper.getArpBill(apayBillId);
		RpPayBill payBill = new RpPayBill();
		payBill.setCustomer(apayBill.getCustomer());
		payBill.setDept(apayBill.getDept());
		payBill.setEmp(apayBill.getEmp());
		payBill.setMaker(getCurrentEmp());
		payBill.setAmount(apayBill.getAmount());
		SysAccountPeriod period = getCurrentPeriod();
		payBill.setFyear(period.getFyear());
		payBill.setPeriod(period.getPeriod());
		RpBillDetail detail = new RpBillDetail();
		RpContact contact = contactMapper.findRpContactByArpBill(apayBillId);
		if(contact != null) {
			detail.setContact(contact);
			detail.setSettleAmount(contact.getRemAmount());
			payBill.setAmount(contact.getRemAmount());
		} else {
			detail.setArpBill(apayBill);
			detail.setSettleAmount(apayBill.getAmount());
		}
		payBill.getDetails().add(detail);
		return payBill;
	}

	public List<RpRecBill> getRecBillList(Date begDate, Date endDate,
			Integer custId) {
		return rpBillMapper.getRpBillListWithCust(begDate, endDate, custId, 1);
	}

	public List<RpPayBill> getPayBillList(Date begDate, Date endDate,
			Integer custId) {
		return rpBillMapper.getRpBillListWithCust(begDate, endDate, custId, -1);
	}	

	public List<RpArpBill> getUnSettleARecBill(Integer custId) {
		return arpBillMapper.getUnSettleArpBill(custId, 1);
	}
	
	public List<RpArpBill> getUnSettleAPayBill(Integer custId) {
		return arpBillMapper.getUnSettleArpBill(custId, -1);
	}

	public List<RpRecBill> getUnSettleRecBill(Integer custId) {
		return rpBillMapper.getUnCheckBills(custId, 1);
	}

	public List<RpPayBill> getUnSettlePayBill(Integer custId) {
		return rpBillMapper.getUnCheckBills(custId, -1);
	}

	public List<RpRecBill> getUnSettleRefundRecBill(Integer custId) {
		return rpBillMapper.getUnCheckRefundBills(custId, 1);
	}

	public List<RpPayBill> getUnSettleRefundPayBill(Integer custId) {
		return rpBillMapper.getUnCheckRefundBills(custId, -1);
	}

	public PagingResult<RpRecBill> getRecBillList(Date begDate, Date endDate,
			int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", 1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListCount", 
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListPaging", 
				params, rows);
	}

	public PagingResult<RpRecBill> getRecBillList(Date begDate, Date endDate,
			Integer custId, int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", 1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		params.put("custId", custId);
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListWithCustCount", 
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListWithCustPaging", 
				params, rows);
	}

	public PagingResult<RpPayBill> getPayBillList(Date begDate, Date endDate,
			int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", -1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListCount", 
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListPaging", 
				params, rows);
	}

	public PagingResult<RpPayBill> getPayBillList(Date begDate, Date endDate,
			Integer custId, int start, int size) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", -1);
		params.put("begDate", begDate);
		params.put("endDate", endDate);
		params.put("custId", custId);
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListWithCustCount", 
				"com.thinkgsp.jxc.persistence.RpBillMapper.getRpBillListWithCustPaging", 
				params, rows);
	}
}
